#include "../../communal/Communication.hpp"
#include "../../communal/Abstract.hpp"
#include "../../communal/Message.hpp"
#include"../../communal/Dispatcher.hpp"
#include"../../client/RpcCaller.hpp"
#include"../../client/Requestor.hpp"

void c(const Json::Value & res)
{
    LOG(LogLevel::DEBUG)<<"res :"<< res.asInt();
}


int main()
{
    auto rq = std::make_shared<xu::client::Requestor>();
    auto rc  = std::make_shared<xu::client::RpcCaller>(rq);
    auto dp = std::make_shared<xu::Dispatcher>();

    auto cb = std::bind(&xu::client::Requestor::OnRequest,rq.get(),std::placeholders::_1,std::placeholders::_2);
    dp->registerHandler<xu::RpcResponse>(xu::MType::RSP_RPC,cb);


    xu::BaseClient::Ptr bc = xu::ModuoClientFactory::create("127.0.0.1",9999);
    auto msg_cb = std::bind(&xu::Dispatcher::onMessage,dp.get(),std::placeholders::_1,std::placeholders::_2);
    bc->SetMessageCallback(msg_cb);
    bc->Connect();

    auto conn = bc->Connection();

    Json::Value params, result;
    params["num1"] = 11;
    params["num2"] = 22;
    bool ret = rc->Call(conn,"Add",params,result);
    if (ret != false) {
        LOG(LogLevel::DEBUG)<<"res :"<<result.asInt();
    }

    Json::Value params1;
    std::future<Json::Value> res;
    params1["num1"] = 33;
    params1["num2"] = 22;
    ret = rc->Call(conn,"Add",params1,res);
    if (ret != false) {
        LOG(LogLevel::DEBUG)<<"res :"<< res.get().asInt();
    }

    Json::Value params2;
    params1["num1"] = 33;
    params1["num2"] = 44;
    ret = rc->Call(conn,"Add",params1,c);

    sleep(10);
    return 0;
}








// void RpcMessageCallback(const xu::BaseConnection::Ptr &bc, xu::BaseMessage::Ptr &mes)
// {
//     std::string body = mes->Serialization();
//     LOG(LogLevel::DEBUG)<<mes->Id() <<" : "<< (int)mes->Mtype()<<" : "<<body;
// }

// int main()
// {
//     xu::BaseClient::Ptr bc = xu::ModuoClientFactory::create("127.0.0.1",9999);
//     bc->SetMessageCallback(RpcMessageCallback);
//     bc->Connect();

//     for(int i = 2; i >0 ; --i)
//     {
//         auto res = xu::MessageFactory::create<xu::RpcRequest>();
//         res->SetId("11111");
//         res->SetMType(xu::MType::REQ_RPC);
//         res->SetMethod("Add");
//         Json::Value val;
//         val["num1"] = 11;
//         val["num2"] = 22;
//         res->SetParams(val);
//         bc->Send(res);
//         sleep(1);
//     }

//     for(int i = 2; i >0 ; --i)
//     {
//         auto res = xu::MessageFactory::create<xu::TopicRequest>();
//         res->SetId("11111");
//         res->SetMType(xu::MType::REQ_TOPIC);
//         res->SetKey("xu");
//         res->SetMsg("xuzhengyou");
//         res->SetOptype(xu::TopicOptype::TOPIC_CREATE);
//         bc->Send(res);
//         sleep(1);
//     }



    
//     bc->ShutDown();
//     LOG(LogLevel::DEBUG)<<"------------------------------------";
//     sleep(10);
//     return 0;
// }